﻿using Microsoft.AspNetCore.Authorization;
using Swimj.Core.Securities;

namespace Swimj.AspNetCore.Infrastructure.Auth
{
    public class PermissionAuthorizationHandler : AuthorizationHandler<PermissionRequirement>
    {
        private readonly ISecurityUser _currentUser;

        public PermissionAuthorizationHandler(ISecurityUser currentUser) =>
            _currentUser = currentUser;

        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context,
            PermissionRequirement requirement)
        {

            if (_currentUser.IsInPermissions(requirement.Permission))
            {
                context.Succeed(requirement);
            }

            return Task.CompletedTask;
        }
    }
}
